﻿using Furion.CMS.Core;
using Furion.DatabaseAccessor;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Furion.CMS.Domain
{
    [OperateRecord("系统用户")]
    [SuppressChangedListener]
    public class UserInfo : Entity, IEntityTypeBuilder<UserInfo>, IEntitySeedData<UserInfo>
    {
        public UserInfo()
        {
            this.CreatedTime = DateTimeOffset.UtcNow;
            this.Passports = new HashSet<UserPassport>();
            this.Roles = new HashSet<RoleInfo>();
            this.Organizations = new HashSet<OrganizationInfo>();
            this.Permissions = new HashSet<PermissionInfo>();
        }

        /// <summary>
        /// 昵称
        /// </summary>
        [MaxLength(100), Required]
        [OperateRecord("昵称", true)]
        public virtual string NickName { get; set; }
        /// <summary>
        /// 性别
        /// </summary>
        public virtual int Gender { get; set; }
        /// <summary>
        /// 头像地址
        /// </summary>
        [MaxLength(100)]
        public virtual string Avatar { get; set; }
        /// <summary>
        /// 启用
        /// </summary>
        [OperateRecord("是否启用")]
        public virtual bool Enable { get; set; }
        /// <summary>
        /// 电子邮件
        /// </summary>
        [MaxLength(100)]
        [OperateRecord("电子邮件")]
        public virtual string Email { get; set; }
        /// <summary>
        /// 电子邮件是否确认
        /// </summary>
        public virtual bool EmailConfirmed { get; set; }
        /// <summary>
        /// 手机号
        /// </summary>
        [MaxLength(14)]
        [OperateRecord("手机号")]
        public virtual string PhoneNumber { get; set; }
        /// <summary>
        /// 手机号是否确认
        /// </summary>
        public virtual bool PhoneNumberConfirmed { get; set; }
        /// <summary>
        /// 是否是种子数据
        /// </summary>
        public bool HasSeed { get; set; }
        /// <summary>
        /// 通行证
        /// </summary>
        public virtual ICollection<UserPassport> Passports { get; set; }
        /// <summary>
        /// 角色
        /// </summary>
        public virtual ICollection<RoleInfo> Roles { get; set; }
        /// <summary>
        /// 所属机构
        /// </summary>
        public virtual ICollection<OrganizationInfo> Organizations { get; set; }
        /// <summary>
        /// 独立权限
        /// </summary>
        public virtual ICollection<PermissionInfo> Permissions { get; set; }

        public void Configure(EntityTypeBuilder<UserInfo> entityBuilder, DbContext dbContext, Type dbContextLocator)
        {
            entityBuilder.UseDefaultTableNameDefine();

            entityBuilder.HasMany(p => p.Passports)
                .WithOne(p => p.User)
                .HasForeignKey(p => p.UserId)
                .OnDelete(DeleteBehavior.Cascade);

            entityBuilder.HasMany(p => p.Roles)
                .WithMany(p => p.Users)
                .UsingEntity<RoleAuthorized>(
                    r => r.HasOne(x => x.Role).WithMany().HasForeignKey(x => x.RoleId),
                    l => l.HasOne(x => x.User).WithMany().HasForeignKey(x => x.UserId)
                );

            entityBuilder.HasMany(p => p.Permissions)
                .WithMany(p => p.Users)
                .UsingEntity<UserPermission>(
                    r => r.HasOne(x => x.Permission).WithMany().HasForeignKey(x => x.PermissionCode),
                    l => l.HasOne(x => x.User).WithMany().HasForeignKey(x => x.UserId)
                );
        }

        public IEnumerable<UserInfo> HasData(DbContext dbContext, Type dbContextLocator)
        {
            string nickName = "Furion CMS";

#if DEBUG
            nickName = "超级管理员";
#endif
            var seedData = new List<UserInfo>();
            seedData.Add(new UserInfo
            {
                Id = 1,
                NickName = nickName,
                Gender = 1,
                Email = "409408401@qq.com",
                EmailConfirmed = true,
                PhoneNumber = "18328408303",
                PhoneNumberConfirmed = true,
                Avatar = "https://gw.alipayobjects.com/zos/antfincdn/XAosXuNZyF/BiazfanxmamNRoxxVxka.png",
                Enable = true,
                HasSeed = true
            });
            return seedData;
        }
    }
}
